package com.morehair.service.wx.impl;

import com.github.pagehelper.PageInfo;
import com.morehair.bean.bo.wx.WxBaseReq;
import com.morehair.bean.pojo.Topic;
import com.morehair.bean.pojo.TopicExample;
import com.morehair.bean.vo.wx.WxBaseData;
import com.morehair.bean.vo.wx.WxTopicDetailVO;
import com.morehair.mapper.TopicMapper;
import com.morehair.service.wx.WxTopicService;
import com.morehair.utils.WxPageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: DLX
 * @Date: 2021/6/8 14:49
 * @Description:
 **/
@Service
@Transactional
public class WxTopicServiceImpl implements WxTopicService {

    @Autowired
    TopicMapper topicMapper;

    @Override
    public WxBaseData<Topic> query(WxBaseReq wxBaseReq) {
        WxPageHelper.startPage(wxBaseReq);

        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();

        criteria.andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(topicExample);

        PageInfo<Topic> topicPageInfo = new PageInfo<>(topics);
        long size = topicPageInfo.getTotal();

        return WxBaseData.ok(topics, size);
    }

    @Override
    public WxTopicDetailVO getTopicDetail(Integer id) {

        Topic topic = topicMapper.selectByPrimaryKey(id);
        List<?> goods = topic.getGoods();
        WxTopicDetailVO wxTopicDetailVO = new WxTopicDetailVO();
        wxTopicDetailVO.setTopic(topic);
        wxTopicDetailVO.setGoods(goods);

        return wxTopicDetailVO;
    }

    @Override
    public List<Topic> getTopicRelated(Integer id) {
        List<Topic> topics = topicMapper.selectRelatedTopics(id);
        return topics;
    }
}
